﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Edge.Business.Entities;
using Edge.Common.ComponentModel.Parameters;
using Edge.Common.Services;
using Edge.Common.Utils;
using Iesi.Collections.Generic;
using Shop.Business.Domain.Orders;

namespace Shop.Business.Domain.Customers
{
    public enum PermissionType
    {
        // magic permissions
        UserManagement = -1,
        SurveyDesigner = -2,
        ReportDesigner = -3,
        Surveys = -4,
        Reports = -5,
        EditOrTranslateForm = -6,
        AdminOnly = -7,
        RimReports = -8,



        // Area User Management
        EditUsers = 1,
        DeleteUsers = 2,
        EditGroups = 3,
        DeleteGroups = 4,
        EditRoles = 5,
        DeleteRoles = 6,

        // Area Survey Designer
        EditForms = 7,
        PublishUnpublishForms = 8,
        TranslateForms = 9,
        EditViews = 10,
        EditPublicQuestions = 11,
        EditChoicesets = 23,

        // Area Report Designer
        ConfigureReports = 12,
        DeleteReports = 13,
        PublishUnpublishReports = 14,

        // Area Event Request
        MasterApproved = 19,

        // Area Reports
        RunReports = 20,
        SaveReports = 21,
        ViewSavedReports = 22,
        DeleteSavedReports = 24,

        // Area Surveys
        CreateRecords = 25,
        EditRecords = 15,
        DeleteRecords = 16,
        TranslateRecords = 18,

        // Rim Reports (Availability Calendars)
        CalendarBU = 26,
        CalendarManagement = 27
    }
    
    /// <summary>
    /// Users object for NHibernate mapped table 'users'.
    /// </summary>
    [Serializable]
    public class Customer : UserEntity<int>
    {

        protected string firstName;
        protected string lastName;
        protected bool sex;
        protected string phone;
        protected int discount;
        protected DateTime birthday = DateTime.Now;
        protected string note;
        protected decimal credits;
        protected string guid;
        protected CustomerRole role;
        protected ISet<CustomerAddress> customerAddress = new HashedSet<CustomerAddress>();
        protected bool rememberMe = true;

        [WebParameterAttribute("name", IsRequired = false)]
        public virtual string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        [WebParameterAttribute("lastName", IsRequired = false)]
        public virtual string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        public virtual string FullName
        {
            get { return firstName + " "+ lastName; }
        }

        public virtual string FullNameDdl
        {
            get { return Id+ ". " + Email +": "+ firstName + " " + lastName; }
        }

//        public virtual bool Sex
//        {
//            get { return sex; }
//            set { sex = value; }
//        }

        public virtual bool RememberMe
        {
            get { return rememberMe; }
            set { rememberMe = value; }
        }

        [WebParameterAttribute("sex", IsRequired = false)]
        public virtual SexType Sex
        {
            get 
            {
                return sex ? SexType.Female : SexType.Male;
            }
            set
            {
                sex = (value == SexType.Female) ? true : false;
            }
        }


        [WebParameterAttribute("phone", IsRequired = false)]
        public virtual string Phone
        {
            get { return phone; }
            set { phone = value; }
        }

        public virtual int Discount
        {
            get { return discount; }
            set { discount = value; }
        }


        [WebParameterAttribute("birthday", IsRequired = false)]
        public virtual DateTime Birthday
        {
            get { return birthday; }
            set { birthday = value; }
        }

        public virtual string Note
        {
            get { return note; }
            set { note = value; }
        }

        public virtual decimal Credits
        {
            get { return credits; }
            set { credits = value; }
        }

        public virtual string Guid
        {
            get { return guid; }
            set { guid = value; }
        }

        public virtual Order DraftOrder
        {
            get
            {
                Order order = new Order();
                return order;
            }
        }

        public virtual ISet<CustomerAddress> Address
        {
            get
            {
                return customerAddress;
            }
            set { customerAddress = value; }
        }

        [Browsable(true),
         Description("Статус"),
         ReadOnly(true)]
        public virtual CustomerRole Role
        {
            get { return role; }
            set { role = value; }
        }

        public Customer()
        {
        }

        public virtual CustomerAddress DefaultAddress
        {
            get
            {
                CustomerAddress ret = null;
                List<CustomerAddress> list = CollectionsUtils.Reverse(Address);
                if (list.Count > 0)
                {
                    if (list.Count == 1)
                        ret = list.First();
                    else
                    {

                        try
                        {
                            ret = list.Single(a => a.Default);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                return ret;
            }
        }

        public Customer(string email, string passwrd, string firstName, string lastName, bool sex, string phone, DateTime birthday, CustomerRole role)
        {
            this.Email = email;
            this.Passwrd = passwrd;
            this.firstName = firstName;
            this.lastName = lastName;
            this.sex = sex;
            this.phone = phone;
            this.birthday = birthday;
            this.role = role;
        }
    }

    public enum SexType
    {
        [Description("Мужской")]
        Male = 0,
        [Description("Женский")]
        Female = 1,

    }
}